package com.xy.biz.base.controller;

import com.xy.biz.base.client.ExpressClient;
import com.xy.biz.base.domain.converter.ExpressConverter;
import com.xy.biz.base.domain.query.ExpressQuery;
import com.xy.biz.base.domain.req.GetExpressByNameRequest;
import com.xy.biz.base.domain.req.GetExpressRequest;
import com.xy.biz.base.domain.req.ListExpressRequest;
import com.xy.biz.base.domain.req.SaveExpressRequest;
import com.xy.biz.base.domain.resp.GetExpressResponse;
import com.xy.biz.base.domain.resp.SfExpressPropertiesResponse;
import com.xy.biz.base.service.ExpressService;
import com.xy.lang.domain.Page;
import com.xy.lang.domain.Result;
import com.xy.lang.validation.SaveChecks;
import com.xy.lang.validation.UpdateChecks;
import com.xy.lang.validation.annotation.Validatable;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Collection;
import java.util.List;

/**
 * @author Greekn Zhang
 * @date 2021/8/5 14:17
 * @description
 */
@Api(tags = "快递服务")
@Slf4j
@RestController
@RequestMapping("/api/base/express")
public class ExpressController implements ExpressClient {

    @Autowired
    private ExpressService expressService;
/*
    @ApiOperation(value = "查询快递")
    @Validatable
    @PostMapping(value = "/get")
    public Result<?> getExpressInfo(@RequestBody GetExpressRequest request) throws Exception {
        return expressService.getExpress(request);
    }*/

    @Validatable(groups = {SaveChecks.class})
    @PostMapping(value = "/save")
    public Result<?> saveExpress(@RequestBody SaveExpressRequest request) {
        expressService.saveExpress(ExpressConverter.toBO(request));
        return Result.success();
    }

    @PostMapping(value = "/get_by_express")
    public Result<SfExpressPropertiesResponse> getByExpress(@RequestBody GetExpressByNameRequest request) {
        return expressService.getByExpress(request);
    }

    @Validatable(groups = {SaveChecks.class})
    @PostMapping(value = "/save_all")
    public Result<?> saveAllExpress(@RequestBody Collection<SaveExpressRequest> collection) {
        expressService.saveAllExpress(ExpressConverter.toBO(collection));
        return Result.success();
    }

    @Validatable(groups = {UpdateChecks.class})
    @PostMapping(value = "/update")
    public Result<?> updateExpress(@RequestBody SaveExpressRequest request) {
        expressService.updateExpress(ExpressConverter.toBO(request));
        return Result.success();
    }

    @Validatable
    @PostMapping(value = "/delete")
    public Result<?> deleteExpress(@RequestBody GetExpressRequest request) {
        expressService.deleteExpress(request.getExpressId());
        return Result.success();
    }

    @PostMapping(value = "/list_by_ids")
    @Override
    public Result<List<GetExpressResponse>> listExpressById(@RequestBody Collection<Long> collection) {
        return Result.success(ExpressConverter.toResponse(expressService.listExpressById(collection)));
    }

    @PostMapping(value = "/list")
    @Override
    public Result<List<GetExpressResponse>> listExpress(@RequestBody ListExpressRequest request) {
        return Result.success(ExpressConverter.toResponse(expressService.listExpress(ExpressConverter.toQuery(request))));
    }

    @PostMapping(value = "/count")
    @Override
    public Result<Integer> countExpress(@RequestBody ListExpressRequest request) {
        return Result.success(expressService.countExpress(ExpressConverter.toQuery(request)));
    }

    @PostMapping(value = "/page")
    @Override
    public Result<Page<GetExpressResponse>> pageExpress(@RequestBody ListExpressRequest request) {
        ExpressQuery query = ExpressConverter.toQuery(request);
        List<GetExpressResponse> list = ExpressConverter.toResponse(expressService.listExpress(query));
        int count = expressService.countExpress(query);
        return Result.success(new Page<>(list, count));
    }
}
